#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# 合并得到的host文件，降序排列后输出

import datetime
try:
    host_regular = open("E:\Hosts\\xuanwulab\host_from_regular.txt", "r").read().split('\n')
    host_short = open("E:\Hosts\\xuanwulab\host_from_short.txt", "r").read().split('\n')

    hostlist = host_regular + host_short

    for i in hostlist:
        if '' in hostlist:
            hostlist.remove('')

    hosts = {}
    for x in hostlist:
        if x in hosts:
            hosts[x] += 1
        else:
            hosts[x] = 1
        # host按照出现频率降序排列
    sorted_hosts = sorted(hosts.items(), key=lambda item: item[1], reverse=True)

    now = datetime.datetime.now()
    file = open('E:\Hosts\\xuanwulab\host_xunawulab_' + now.strftime('%Y%m%d%H%M') + '.txt', 'w')
    file.write("不同host总数为：" + str(len(sorted_hosts)) + '\n')
    for i in range(len(sorted_hosts)):
        file.write(str(sorted_hosts[i][0]) + ": " + str(sorted_hosts[i][-1]) + '\n')
    file.close()

except Exception as e:
    print(e)






